package Comp355Final.main;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class GUIBookSelector extends Activity{
	private Utility myutility;
	private Config myconfig;
	private FileIO fileio;
	private String curr_dest;
	private ArrayList<String> file_list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        String curr_folder = null;
        if(extras!=null)
        {
        curr_folder=extras.getString("curr_folder");
        }
        if(curr_folder==null)
        	curr_folder="";
    	myconfig = new Config(this);
    	fileio = new FileIO(true,curr_folder);
    	myutility = new Utility();
    	ArrayList<String> path_list;
    
    	
    	path_list = fileio.scan_folder();
    	
    	
    	ArrayList<String> folder_list = fileio.is_folder(path_list);
    	file_list = fileio.filter_allExt(path_list);
    //	Utility.quick_dialog(path_list.size()+" "+folder_list.size(), this);
    	ScrollView scllview = new ScrollView(this);
    	ScrollView.LayoutParams scllview_param = new ScrollView.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
    	LinearLayout Llay = new LinearLayout(this);
        LinearLayout.LayoutParams Llay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        scllview.setBackgroundColor(Color.WHITE);
        scllview.setBackgroundDrawable(myconfig.return_bg());
        Llay.setOrientation(LinearLayout.VERTICAL);
        String parentDir = fileio.getParentDirectory();

        if(parentDir!=null&&(fileio.getParentDirectory().contains(FileIO.getRootDirectory())))
        {
          	LinearLayout tmp_layout = new LinearLayout(this);
            LinearLayout.LayoutParams tmp_layout_param = new LinearLayout.LayoutParams(
            		ViewGroup.LayoutParams.FILL_PARENT,
            		ViewGroup.LayoutParams.WRAP_CONTENT);
        	TextView tmp_txtview = new TextView(this);
        	tmp_txtview.setText("..");
         	tmp_txtview.setTextColor(Color.BLACK);
        	tmp_layout.setOrientation(LinearLayout.HORIZONTAL);
        	tmp_layout.setClickable(true);
           	PanelOnTouchListener potl = new PanelOnTouchListener(tmp_txtview);
           	GUIBookSelectorListener guibsl = new GUIBookSelectorListener(this,parentDir,true);
           	tmp_layout.setOnClickListener(guibsl);
        	tmp_layout.setOnTouchListener(potl);
        	tmp_layout.addView(tmp_txtview);
        	Llay.addView(tmp_layout,tmp_layout_param);
        }
        for(int i=0;i<folder_list.size();i++)
        {
          	LinearLayout tmp_layout = new LinearLayout(this);
            LinearLayout.LayoutParams tmp_layout_param = new LinearLayout.LayoutParams(
            		ViewGroup.LayoutParams.FILL_PARENT,
            		ViewGroup.LayoutParams.WRAP_CONTENT);
        	TextView tmp_txtview = new TextView(this);
        	tmp_txtview.setText(folder_list.get(i));
         	tmp_txtview.setTextColor(Color.BLACK);
        	tmp_layout.setOrientation(LinearLayout.HORIZONTAL);
        	tmp_layout.setClickable(true);
           	PanelOnTouchListener potl = new PanelOnTouchListener(tmp_txtview);
           	GUIBookSelectorListener guibsl = new GUIBookSelectorListener(this,folder_list.get(i),true);
           	tmp_layout.setOnClickListener(guibsl);
        	tmp_layout.setOnTouchListener(potl);
        	tmp_layout.addView(tmp_txtview);
        	Llay.addView(tmp_layout,tmp_layout_param);
        }
        for(int i=0;i<file_list.size();i++)
        {
          	LinearLayout tmp_layout = new LinearLayout(this);
            LinearLayout.LayoutParams tmp_layout_param = new LinearLayout.LayoutParams(
            		ViewGroup.LayoutParams.FILL_PARENT,
            		ViewGroup.LayoutParams.WRAP_CONTENT);
        	TextView tmp_txtview = new TextView(this);
        	tmp_txtview.setText(file_list.get(i));
         	tmp_txtview.setTextColor(Color.BLACK);
        	tmp_layout.setOrientation(LinearLayout.HORIZONTAL);
        	tmp_layout.setClickable(true);
        	tmp_layout.setId(i);
           	PanelOnTouchListener potl = new PanelOnTouchListener(tmp_txtview);
           	GUIBookSelectorListener guibsl = new GUIBookSelectorListener(this,file_list.get(i),false);
           	tmp_layout.setOnClickListener(guibsl);
        	tmp_layout.setOnTouchListener(potl);
        	tmp_layout.addView(tmp_txtview);
        	Llay.addView(tmp_layout,tmp_layout_param);
        }
    	this.setTitle(fileio.get_sdcard_root()+curr_folder);
        scllview.addView(Llay, Llay_param);
        this.setContentView(scllview, scllview_param);
    }
    private String download_str;
    public void download_file(String id)
    {
    	download_str=id;
    	
    	this.showDialog(DIALOG_RENAME_ENTRY);
    }
    private static final int DIALOG_RENAME_ENTRY = 8;
    @Override   
    protected Dialog onCreateDialog(int id) {
    	switch(id){
        case DIALOG_RENAME_ENTRY:
        	// This example shows how to add a custom layout to an AlertDialog
        	LayoutInflater factory2 = LayoutInflater.from(this);
        	final EditText search_box2 = new EditText(this);
        	final View textEntryView2 = search_box2;
        	return new AlertDialog.Builder(GUIBookSelector.this)
        	//.setIconAttribute(android.R.drawable.)
        	.setTitle("Please name the book first.")
        	.setView(textEntryView2)
        	.setPositiveButton("ok", new DialogInterface.OnClickListener() {
        		public void onClick(DialogInterface dialog, int whichButton) {
        			String tmp_string = search_box2.getText().toString();
        			if(!tmp_string.equals(""))
        			{
        
        				File org_file = new File(GUIBookSelector.this.download_str);
        				String dst_folder =FileIO.getRootDirectory()+"/bookshelf/"+tmp_string;
        				if(!GUIBookSelector.this.fileio.check_Folderexisted(dst_folder))
        					GUIBookSelector.this.fileio.make_folder(dst_folder);
        				
        				File dst_file = new File(dst_folder+"/"+org_file.getName());
        				if(!GUIBookSelector.this.fileio.check_Fileexisted(dst_folder+"/"+org_file.getName()))
        				{
	        				if(FileIO.copy(org_file, dst_file))
	        				{
	        					Utility.quick_dialog("File is copied to"+dst_folder+"/"+org_file.getName(), GUIBookSelector.this);
	        				}
	        				else
	        				{
	        					Utility.quick_dialog("File cannot copied to"+dst_folder+"/"+org_file.getName(), GUIBookSelector.this);
	        				}
        				}
        			}
        		} 
        		})
        		.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        			public void onClick(DialogInterface dialog, int whichButton) {
        				search_box2.setText("");
        				/* User clicked cancel so do some stuff */
        				} 
        			})
        			.create();	

    	}
    	return null;
    	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 0, 0, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert); 
    	return super.onCreateOptionsMenu(menu); 
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { 
    	if(item.getItemId()==0)
    	{
    		Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
    		this.startActivity(int_ent);
    		finish();
    	}
    	return super.onOptionsItemSelected(item);
    }
    
}
